package com.movecar.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.movecar.R;
import com.movecar.model.GetGroupBean.Comments;
import com.movecar.model.GroupBean;
import com.movecar.utils.Alert;
import com.movecar.utils.AppConfig;
import com.movecar.utils.DataStore;
import com.movecar.utils.HttpPostRequestAsyncTask;
import com.movecar.utils.RequestResultCallback;
import com.movecar.widget.CustomProgressDialog;

/**
 * 添加讨论
 * 
 * @author Zhao.JQ
 * 
 */
public class GroupDiscussActivity extends Activity implements OnClickListener {

	private String messageId = "";
	private EditText content;
	private ListView mListView;
	private ArrayList<Comments> commentsData = null;
	private CustomProgressDialog progressDialog = null;
	Button right;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.group_discuss);
		initView();
	}

	@SuppressWarnings("unchecked")
	private void initView() {

		Button back = (Button) findViewById(R.id.btn_left);
		right = (Button) findViewById(R.id.btn_right);
		back.setVisibility(View.VISIBLE);
		back.setOnClickListener(this);
		right.setVisibility(View.VISIBLE);
		right.setBackgroundResource(R.drawable.btn_confirm);
		right.setOnClickListener(this);
		mListView = (ListView) findViewById(R.id.mListView);
		content = (EditText) findViewById(R.id.content);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("发表讨论");
		stopProgressDialog();
		if (this.getIntent() != null) {
			messageId = getIntent().getStringExtra("messageId");
			commentsData = (ArrayList<Comments>) getIntent().getSerializableExtra("comments");
			if (commentsData != null && commentsData.size() > 0) {
				mListView.setVisibility(View.VISIBLE);
				DataAdapter dataAdapter = new DataAdapter(GroupDiscussActivity.this, commentsData);
				mListView.setAdapter(dataAdapter);
			}
		}

	}

	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_left:
			finish();
			break;
		case R.id.btn_right:

			if (!"".equals(content.getText().toString()) && !"".equals(messageId)) {
				jsonData();
			} else {
				Toast.makeText(getApplicationContext(), "请输入讨论内容", Toast.LENGTH_SHORT).show();
			}
			break;

		default:
			break;
		}

	}

	@SuppressWarnings("unchecked")
	private void jsonData() {

		startProgressDialog();
		right.setEnabled(false);
		List<NameValuePair> params = new ArrayList<NameValuePair>(); // 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "addComment")); // 添加要传递的参数
		params.add(new BasicNameValuePair("messageId", messageId));
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		params.add(new BasicNameValuePair("comment", content.getText().toString().trim()));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {

				String result = (String) object;
				stopProgressDialog();
				if ("0".equals(result)) {
					Toast.makeText(getApplicationContext(), "发表成功", Toast.LENGTH_SHORT).show();
					setResult(RESULT_OK);
					finish();
				} else {
					Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
				}

			}

			@Override
			public void onFail(String exception) {

				stopProgressDialog();
				Alert.error(GroupDiscussActivity.this,"与服务器连接失败，请检查网络");

			}
		}, GroupBean.class, AppConfig.GROUP).execute(params);
	}

	class DataAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		private ViewHolder viewHolder = null;
		private ArrayList<Comments> commentsData;

		DataAdapter(Activity activity, ArrayList<Comments> commentsData) {

			this.commentsData = commentsData;
			inflater = LayoutInflater.from(activity);
		}

		@Override
		public int getCount() {

			return commentsData.size();
		}

		@Override
		public Object getItem(int arg0) {

			return commentsData.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {

			return arg0;
		}

		@Override
		public View getView(final int position, View view, ViewGroup arg2) {

			if (view == null) {
				viewHolder = new ViewHolder();
				view = inflater.inflate(R.layout.look_discuss_item, null);

				viewHolder.userNickName = (TextView) view.findViewById(R.id.userName);
				viewHolder.content = (TextView) view.findViewById(R.id.content);
				viewHolder.createDate = (TextView) view.findViewById(R.id.createDate);

				view.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) view.getTag();
			}
			String nickName = commentsData.get(position).getUserNickName().toString();

			viewHolder.userNickName.setText(nickName);
			viewHolder.content.setText(commentsData.get(position).getContent());
			viewHolder.createDate.setText(commentsData.get(position).getCreateDate());

			// }
			return view;
		}

		class ViewHolder {

			TextView userNickName, content, createDate;
			LinearLayout myLayout;
			ImageView group_img, isjoin;
			Button group_join, group_discuss;
		}
	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}
}
